﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ServiceModel.DomainServices.Client;
using Telerik.Windows.Controls;
using ZL.Push.Web;


namespace ZL.Push
{
    public partial class CopyToFriendControlBase : BaseReadonlyControl<tbl_friend>
    {
    }

    public partial class CopyToFriendControl : CopyToFriendControlBase
    {
        protected List<int> _lst_friend_id = new List<int>();

        public CopyToFriendControl()
        {
            InitializeComponent();
            base.InitializeComponent("tbl_friend");
        }


        public void search(int metadata_id)
        {
            // 查询user_id
            _lst_friend_id.Clear();

            App app = App.Current as App;
            EntityQuery<tbl_metadata> query = app.context.GetTbl_metadataQuery().Where(c => c.original_metadata_id == metadata_id);
            app.context.Load(query, (lo) =>
            {
                if (lo.HasError)
                {
                    MessageBox.Show(lo.Error.Message);
                }
                else
                {
                    foreach (tbl_metadata tbl2 in lo.Entities)
                    {
                        _lst_friend_id.Add(tbl2.user_id);
                    }

                    _vm.lambda = _create_lamda();
                    _vm.OnSearch();
                }
            }, null);
        }


        protected override Expression<Func<tbl_friend, bool>> _create_lamda()
        {
            List<SearchParameter> lst = new List<SearchParameter>();

            if (_lst_friend_id == null || _lst_friend_id.Count == 0)
            {
                return App.s_create_no_result_lamda(typeof(tbl_friend)) as Expression<Func<tbl_friend, bool>>;
            }

            foreach (int friend_id in _lst_friend_id)
            {
                lst.Add(new SearchParameter()
                {
                    Relation = EnumRelation.Or,
                    PropertyName = "friend_id",
                    Operator = EnumOperator.Equal,
                    PropertyValue = friend_id,
                    TypeOfValue = typeof(int),
                });
            }

            return App.s_create_lamda_with_user(typeof(tbl_friend), null, ref lst)
                        as Expression<Func<tbl_friend, bool>>;
        }


        #region datagrid相关函数
        protected override void _datagrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            StackPanel sp = _datagrid.Columns[0].GetCellContent(e.Row) as StackPanel;
            TextBlock cell = sp.Children.ElementAt(0) as TextBlock;
            if (_datapager != null)
            {
                cell.Text = (_datapager.PageIndex * _datapager.PageSize + e.Row.GetIndex() + 1).ToString() + " ";
            }
            else
            {
                cell.Text = (e.Row.GetIndex() + 1).ToString() + " ";
            }
        }

        protected override void _datagrid_OnDoubleClicked(object sender, EventArgs e)
        {
            if (_datagrid.SelectedItem == null)
            {
                return;
            }

            FriendWindow win = new FriendWindow(AddOrEditType.eNone);
            win.DataContext = _datagrid.SelectedItem as tbl_friend;
            win.Show();
        }
        #endregion
    }
}